/*
 * key_dirver.c
 *
 *  Created on: Oct 28, 2021
 *      Author: 王慧云
 */

#include "key_dirver.h"

#define KEY1  HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)
#define KEY2  HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)
#define KEY3  HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)
#define KEY4  HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)
#define KEY5  HAL_GPIO_ReadPin(KEY5_GPIO_Port,KEY5_Pin)


uint8_t keySta[] = {1,1,1,1,1};
uint8_t keyMap[] = {2,1,4,5,3};

void KEY_Scan(void)
{
	uint8_t i;
	static uint8_t key_buff[] ={0xff,0xff,0xff,0xff,0xff};

	key_buff[0]= (key_buff[0] << 1) | KEY1;
	key_buff[1]= (key_buff[1] << 1) | KEY2;
	key_buff[2]= (key_buff[2] << 1) | KEY3;
	key_buff[3]= (key_buff[3] << 1) | KEY4;
	key_buff[4]= (key_buff[4] << 1) | KEY5;
	for(i = 0; i < 5; i ++)
	{
		if((key_buff[i] & 0xff) == 0xff)
		{
			keySta[i] = 1;
		}

		else if((key_buff[i] & 0xff) == 0x00)
		{
			keySta[i] = 0;
		}
		else{}
	}
}

#include <stdio.h>
uint8_t Get_KeyVal(void)
{
	uint8_t i, res = 0;
	static uint8_t backup[] = {1,1,1,1,1};

	for(i = 0; i < 5; i ++)
	{
		if(backup[i] != keySta[i])
		{
			if(backup[i] != 0)
			{
				res = keyMap[i];
				//printf("%d\r\n",res);
			}
			else
			{
				res = 0;
			}
		}
		backup[i] = keySta[i];
	}
	return res;
}



